package cc.smarnet.caihou.phone;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import cc.smarnet.caihou.R;
import cc.smarnet.caihou.phone.fragment.HomeFragment;
import cc.smarnet.caihou.phone.fragment.StatisticsFragment;
import cc.smarnet.caihou.phone.fragment.UserFragment;

/**
 * 主页面的activity
 * */
public class MainPageActivity extends AppCompatActivity implements View.OnClickListener{
    private HomeFragment homeFragment;
    private StatisticsFragment statisticsFragment;
    private UserFragment userFragment;

    private RelativeLayout homeRelativeLayout;
    private RelativeLayout statisticsRelativeLayout;
    private RelativeLayout userRelativeLayout;

    private ImageView homeImageView;
    private ImageView statisticsImageView;
    private ImageView userImageView;

    private TextView homeTextView;
    private TextView statisticsTextView;
    private TextView userTextView;

    private long mExitTime;       //实现“再按一次退出”的记录时间变量

    FragmentManager fragmentManager;

    private String forwardTextColor = "#9EC2F0";// 选中前字体颜色
    private String afterTextColor = "green";// 选中后字体颜色

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_page);

        getSupportActionBar().hide();//隐藏标题栏

        initViews();
        fragmentManager = getSupportFragmentManager();
        setChioceItem(0);
    }

    //完成组件的初始化
    public void initViews() {
        homeRelativeLayout = findViewById(R.id.main_relativelayout_home);
        statisticsRelativeLayout = findViewById(R.id.main_relativelayout_statistics);
        userRelativeLayout = findViewById(R.id.main_relativelayout_user);

        homeImageView = findViewById(R.id.main_image_home);
        statisticsImageView = findViewById(R.id.main_image_statistics);
        userImageView = findViewById(R.id.main_image_user);

        homeTextView = findViewById(R.id.main_text_home);
        statisticsTextView = findViewById(R.id.main_text_statistics);
        userTextView = findViewById(R.id.main_text_user);

        homeRelativeLayout.setOnClickListener(this);
        statisticsRelativeLayout.setOnClickListener(this);
        userRelativeLayout.setOnClickListener(this);
    }
    @Override //点击返回实体键
    public void onBackPressed() {
        if (System.currentTimeMillis() - mExitTime < 2000) {
            super.onBackPressed();
            finish();
        } else {
            mExitTime = System.currentTimeMillis();
            Toast.makeText(MainPageActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.main_relativelayout_home:
                setChioceItem(0);
                break;
            case R.id.main_relativelayout_statistics:
                setChioceItem(1);
                break;
            case R.id.main_relativelayout_user:
                setChioceItem(2);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    //选中一个item后的处理
    public void setChioceItem(int index) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //重置选项+隐藏所有Fragment
        clearChioce();
        hideFragments(fragmentTransaction);
        switch (index) {
            case 0:
                homeImageView.setImageResource(R.drawable.img_main_home_press);
                homeTextView.setTextColor(Color.parseColor(afterTextColor));
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                    fragmentTransaction.add(R.id.main_content, homeFragment);
                } else {
                    fragmentTransaction.show(homeFragment);
                }
                break;

            case 1:
                statisticsImageView.setImageResource(R.drawable.img_main_statistics_press);
                statisticsTextView.setTextColor(Color.parseColor(afterTextColor));
                if (statisticsFragment == null) {
                    statisticsFragment = new StatisticsFragment();
                    fragmentTransaction.add(R.id.main_content, statisticsFragment);
                } else {
                    fragmentTransaction.show(statisticsFragment);
                }
                break;

            case 2:
                userImageView.setImageResource(R.drawable.img_main_user_press);
                userTextView.setTextColor(Color.parseColor(afterTextColor));
                if (userFragment == null) {
                    userFragment = new UserFragment();
                    fragmentTransaction.add(R.id.main_content, userFragment);
                } else {
                    fragmentTransaction.show(userFragment);
                }
                break;
        }
        fragmentTransaction.commit();
    }

    //隐藏所有的Fragment,避免fragment混乱
    private void hideFragments(FragmentTransaction transaction) {
        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (statisticsFragment != null) {
            transaction.hide(statisticsFragment);
        }

        if (userFragment != null) {
            transaction.hide(userFragment);
        }
    }

    //重置所有选项的方法
    public void clearChioce() {
        homeImageView.setImageResource(R.drawable.img_main_home);
        statisticsImageView.setImageResource(R.drawable.img_main_statistics);
        userImageView.setImageResource(R.drawable.img_main_user);

        homeTextView.setTextColor(Color.parseColor(forwardTextColor));
        statisticsTextView.setTextColor(Color.parseColor(forwardTextColor));
        userTextView.setTextColor(Color.parseColor(forwardTextColor));
    }
}